import { Token , AccountDetail } from '../models'
import { getSecond , uuid } from './common'
import { CODE } from '../config/config'
export async function _getAccount(uid,key) {  
    const query       = {}
    query[key]        = uid
    const token       = await Token.findOne({where:query})
    if(token) {
        token.token   = uuid()
        token.ctime   = getSecond()
        await token.save()
    }

    const phone_uid    = token.phone_uid
    const wechat_uid   = token.wechat_uid
    const qq_uid       = token.qq_uid

    let phone_account = null , wechat_account = null , qq_acount = null
    if(phone_uid) {
        phone_account  = await AccountDetail.findOne({where:{uid:phone_uid,status:1}})
    }

    if(wechat_uid) {
        wechat_account  = await AccountDetail.findOne({where:{uid:wechat_uid,status:1}})
    }

    if(qq_uid) {
        qq_acount  = await AccountDetail.findOne({where:{uid:qq_uid,status:1}})
    }
    if(!phone_account) {
        phone_account = {}
    }
    if(!wechat_account) {
        wechat_account = {}
    }
    if(!qq_acount) {
        qq_acount = {}
    }

    const icon     = phone_account.icon || wechat_account.icon || qq_acount.icon
    const nickname = phone_account.nickname || wechat_account.nickname || qq_acount.nickname
    const phone    = phone_account.phone || wechat_account.phone || qq_acount.phone

    return {
        icon,nickname,phone,token:token.token
    }
    
}

export async function _bindPhone(param) { 
    const {phone,wechat_key,qq_key} = param   
    let phone_account  = null
    let wechat_account = null
    let qq_account     = null 
    let phone_uid      = null
    let wechat_uid     = null
    let qq_uid         = null
    let phone_token    = null
    let wechat_token   = null
    let qq_token       = null
    
    phone_account = await Account.findOne({where:{phone,status:1}})
    if(!phone_account) {
        phone_account = await Account.create({phone,uid:uuid()})
    }
    phone_uid = phone_account.uid
    phone_token = await Token.findOne({where:{phone_uid,status:1}})

    if(wechat_key) {
         wechat_account = await Account.findOne({where:{wechat_key,status:1}})
         if(wechat_account) {
             wechat_uid = wechat_account.uid
             wechat_token = await Token.findOne({where:{wechat_uid,status:1}})
         }
    }

    if(qq_key) {
         qq_account = await Account.findOne({where:{qq_key,status:1}})
         if(qq_account) {
             qq_uid = qq_account.uid
             qq_token = await Token.findOne({where:{qq_uid,status:1}})
         }
    }

    if(phone_token) {
        if(phone_token.wechat_uid && wechat_uid) {
            return CODE.ERROR.ERROR_BIND_WECHAT_ALREADY
        }
        if(phone_token.qq_uid && qq_uid) {
            return CODE.ERROR.ERROR_BIND_QQ_ALREADY
        }
        phone_token.wechat_uid = wechat_uid || phone_token.wechat_uid
        phone_token.qq_uid = qq_uid || phone_token.qq_uid
        await phone_token.save()
        if(wechat_token) {
            wechat_token.status = 2
            await wechat_token.save()
        }
        if(qq_token) {
            qq_token.status = 2
            await qq_token.save()
        }
    } else if(wechat_token) { 
        wechat_token.phone_uid = phone_uid
        await wechat_token.save()
    } else {
        qq_token.wechat_uid = wechat_uid
        qq_token.phone_uid = phone_uid
        await qq_token.save()
    }
    return CODE.success()
}